﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace CPPEI.WebApi
{
    public static class HttpMethods
    {
        public static string POST = "POST";
        //public const string DELETE = "DELETE";
        //public const string PUT = "PUT";
        public static string GET = "GET";
    }

    public interface IAction
    {
        /// <summary>
        /// 模式POST，GET，DELETE
        /// </summary>
        string HttpMethod { get; }
        /// <summary>
        /// 是否允许匿名访问
        /// </summary>
        bool IsAnonymous { get; }
        ActionResult Execute(HttpRequest request, SysParams sysParams);
    }

    /// <summary>
    /// action执行的上下文信息
    /// </summary>
    public class ActionContext
    {
        /// <summary>
        /// 用户的id
        /// </summary>
        public string UserId { get; set; }
        /// <summary>
        /// 本次请求的终端的ip地址
        /// </summary>
        public string IP { get; set; }

        public HttpRequest HttpRequest { get; internal set; }
    }

    /*
    /// <summary>
    /// post http://.....
    /// </summary>
    /// <typeparam name="TParam"></typeparam>
    /// <typeparam name="TResult"></typeparam>
    public abstract class PostAction<TParam, TResult> : IAction
    {
        public ActionResult Execute(HttpRequest request, SysParameters sysParameters)
        {
            ActionContext context = new ActionContext();
            context.IP = request.UserHostAddress;

            TParam opParams = (TParam)(typeof(TParam).Assembly.CreateInstance(typeof(TParam).FullName));
            //opParams.ParseForm(request);
            ParameterInjection.Injection4Post(request, opParams);

            ActionResult<TResult> actionResult = OnExecute(context, sysParameters, opParams);

            return new ActionResult(actionResult.ActionVersion, actionResult.IsSccuess, actionResult.ErrorCode, actionResult.Message, actionResult.Data);
        }

        protected abstract ActionResult<TResult> OnExecute(ActionContext context, SysParameters sysParams, TParam opParams);
    }

    /// <summary>
    /// get http://...
    /// </summary>
    /// <typeparam name="TParam"></typeparam>
    /// <typeparam name="TResult"></typeparam>
    public abstract class GetAction<TParam, TResult> : IAction
    {
        public ActionResult Execute(HttpRequest request, SysParameters sysParameters)
        {
            ActionContext context = new ActionContext();
            context.IP = request.UserHostAddress;
            if (!string.IsNullOrEmpty(sysParameters.Token))
            {
                WebApi.u uObj = WebApi.UIDToken.FromToken(sysParameters.Token);
                if (uObj.Validate())
                {
                    if (uObj.ValidateTimeout())
                    {
                        context.UserId = uObj.a;
                    }
                }
            }

            TParam opParams = (TParam)(typeof(TParam).Assembly.CreateInstance(typeof(TParam).FullName));
            try
            {
                ParameterInjection.Injection4Get(request, opParams);
            }
            catch
            {
                return new ActionResult(false, ErrorCode.Common_InvalidParameter, "参数无效", null);
            }

            ActionResult<TResult> actionResult = OnExecute(context, sysParameters, opParams);

            return new ActionResult(actionResult.ActionVersion, actionResult.IsSccuess, actionResult.ErrorCode, actionResult.Message, actionResult.Data);
        }

        protected abstract ActionResult<TResult> OnExecute(ActionContext context, SysParameters sysParams, TParam opParams);
    }

    /// <summary>
    /// 需要认证才能执行的get action
    /// </summary>
    public abstract class AuthGetAction<T1, T2> : IAction where T1 : IActionParams
    {
        public ActionResult Execute(HttpRequest request, SysParameters sysParameters)
        {
            WebApi.u uObj = WebApi.UIDToken.FromToken(sysParameters.Token);
            if (uObj.Validate())
            {
                if (uObj.ValidateTimeout())
                {
                    ActionContext context = new ActionContext();
                    context.IP = request.UserHostAddress;
                    context.UserId = uObj.a;
                    
                    T1 opParams = (T1)(typeof(T1).Assembly.CreateInstance(typeof(T1).FullName));
                    opParams.ParseQueryString(request);

                    ActionResult<T2> actionResult = OnExecute(context, sysParameters, opParams);

                    return new ActionResult(actionResult.ActionVersion, actionResult.IsSccuess, actionResult.ErrorCode, actionResult.Message, actionResult.Data);
                }
                return new ActionResult(false, ErrorCode.Auth_Timeout, string.Empty, null);
            }

            return new ActionResult(false, ErrorCode.Auth_NoAuth, string.Empty, null);
        }

        protected abstract ActionResult<T2> OnExecute(ActionContext context, SysParameters sysParams, T1 opParams);
    }

    /// <summary>
    /// 需要认证才能执行的post action
    /// </summary>
    public abstract class AuthPostAction<T1, T2> : IAction where T1 : IActionParams
    {
        public ActionResult Execute(HttpRequest request, SysParameters sysParameters)
        {
            WebApi.u uObj = WebApi.UIDToken.FromToken(sysParameters.Token);
            if (uObj.Validate())
            {
                if (uObj.ValidateTimeout())
                {
                    ActionContext context = new ActionContext();
                    context.IP = request.UserHostAddress;
                    context.UserId = uObj.a;

                    T1 opParams = (T1)(typeof(T1).Assembly.CreateInstance(typeof(T1).FullName));
                    opParams.ParseForm(request);

                    ActionResult<T2> actionResult = OnExecute(context, sysParameters, opParams);

                    return new ActionResult(actionResult.ActionVersion, actionResult.IsSccuess, actionResult.ErrorCode, actionResult.Message, actionResult.Data);
                }
                return new ActionResult(false, ErrorCode.Auth_Timeout, string.Empty, null);
            }

            return new ActionResult(false, ErrorCode.Auth_NoAuth, string.Empty, null);
        }

        protected abstract ActionResult<T2> OnExecute(ActionContext context, SysParameters sysParams, T1 opParams);
    }
    */
}
